package video.background.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import video.background.Controller.View.MessageView;
import video.background.Dao.MessageDao;
import video.background.Dao.MsgUserDao;
import video.background.Model.Message;
import video.background.Model.MsgUser;
import video.background.Utils.JwtUtils;
import video.background.Utils.Result;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/message")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(tags="系统消息相关接口")
public class MessageController {
    @Autowired
    public MessageDao messageDao;
    @Autowired
    public MsgUserDao msgUserDao;
    @GetMapping("/getMessage")
    @ApiOperation("用户查看系统消息")
    public Result getMessage(@RequestHeader String Authorization){
        //解析token
        Long userId = JwtUtils.decoderToken(Authorization);
        if(userId == 0) return Result.Error("用户数据不全，请检查数据",null);
        //查看消息
        LambdaQueryWrapper<Message> mlqw = new LambdaQueryWrapper<>();
        mlqw.eq(Message::getUserId,userId);
        List<Message> messageList = messageDao.selectList(mlqw);
        List<MessageView> messageViews = new ArrayList<>();
        for (Message message : messageList){
            LambdaQueryWrapper<MsgUser> mulqw = new LambdaQueryWrapper<>();
            mulqw.eq(MsgUser::getMessageId,message.getMessageId()).eq(MsgUser::getUserId,userId);
            MsgUser msgUser = msgUserDao.selectOne(mulqw);
            MessageView messageView = new MessageView(message.getUserName(),message.getTitle(),message.getContent(),msgUser.getTime());
            messageViews.add(messageView);
        }
        return Result.Success("数据查看成功",messageViews);
    }

}
